#!/usr/bin/env pypy

import unittest, os

import playlist_module

from settings import _Settings, BASE_PATH

SETTINGS_TEST_YAML = "settingsTest.yaml"

VALUES_DICT = {
	"cat_sub_lop_abbr": "LOP",
	"data_fav_mult": 2,
	"date_holiday_halloween_start": "Oct 1",
	"date_holiday_halloween_end": "Oct 31",
	"ext_bak": "bak",
	"ext_music": "mp3",
	"favorite_abbr": "F%s",
	"files_cat": '%s.tracklist.gz',
	"flags_favorite": "Favorite",
	"log_type_xdh_logging_debug": True,
	"mpd_host": "localhost",
	"path_data": "data",
	"podcast_pods_faith_title": "Fighting for the Faith",
	"re_catfile": "(.*)\.tracklist\.gz$",
	"tags_album": "Normal",
	"types_flac_name": "FLAC",
	"types_flac_tags_album": "album"
}

class TestSettings(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		test_yaml = """cat:
    sub:
        lop:
            abbr: {cat_sub_lop_abbr}
data:
    fav_mult: {data_fav_mult}
date:
    holiday:
        halloween:
        -   - {date_holiday_halloween_start}
            - {date_holiday_halloween_end}
ext:
    bak: {ext_bak}
    music:
    - {ext_music}
favorite:
    abbr: {favorite_abbr}
files:
    cat: '{files_cat}'
flags:
    favorite: {flags_favorite}
log:
    type:
        xdh_logging:
            debug: {log_type_xdh_logging_debug}
mpd:
    host: {mpd_host}
path:
    data: {path_data}
podcast:
    pods:
        faith:
        -   title: {podcast_pods_faith_title}
re:
    catfile: {re_catfile}
tags:
    album: {tags_album}
types:
    flac:
        name: {types_flac_name}
        tags:
            album: {types_flac_tags_album}
""".format(**VALUES_DICT)

		with open(SETTINGS_TEST_YAML, "w") as f:
			f.write(test_yaml)

	@classmethod
	def tearDownClass(cls):
		os.remove(SETTINGS_TEST_YAML)

		bak = "{filename}.bak".format(filename = SETTINGS_TEST_YAML)

		if os.path.exists(bak):
			os.remove(bak)

	def setUp(self):
		self.settings = _Settings(SETTINGS_TEST_YAML)

	def test_cat_sub_lop_abbr(self):
		self.assertEqual(self.settings.cat.sub.lop.abbr, VALUES_DICT["cat_sub_lop_abbr"])

	def test_data_fav_mult(self):
		self.assertEqual(self.settings.data.fav_mult, VALUES_DICT["data_fav_mult"])

	def test_date_holiday_halloween(self):
		self.assertEqual(set(self.settings.date.holiday.halloween[0]), set([VALUES_DICT["date_holiday_halloween_start"], VALUES_DICT["date_holiday_halloween_end"]]))

	def test_ext_bak(self):
		self.assertEqual(self.settings.ext.bak, VALUES_DICT["ext_bak"])

	def test_ext_music(self):
		self.assertEqual(self.settings.ext.music[0], VALUES_DICT["ext_music"])

	def test_favorite_abbr(self):
		self.assertEqual(self.settings.favorite.abbr, VALUES_DICT["favorite_abbr"])

	def test_favorite_abbr_2(self):
		self.assertEqual(self.settings.favorite.abbr % self.settings.cat.sub.lop.abbr, VALUES_DICT["favorite_abbr"] % VALUES_DICT["cat_sub_lop_abbr"])

	def test_files_cat(self):
		self.assertEqual(self.settings.files.cat, VALUES_DICT["files_cat"])

	def test_flags_favorite(self):
		self.assertEqual(self.settings.flags.favorite, VALUES_DICT["flags_favorite"])

	def test_log_type_xdh_logging_debug(self):
		self.assertEqual(self.settings.log.type.xdh_logging.debug, VALUES_DICT["log_type_xdh_logging_debug"])

	def test_mpd_host(self):
		self.assertEqual(self.settings.mpd.host, VALUES_DICT["mpd_host"])

	def test_path_data(self):
		self.assertEqual(self.settings.path.data, os.path.join(BASE_PATH, VALUES_DICT["path_data"]))

	def test_podcast_pods_faith_title(self):
		self.assertEqual(self.settings.podcast.pods.faith[0].title, VALUES_DICT["podcast_pods_faith_title"])

	def test_re_catfile(self):
		self.assertEqual(self.settings.re.catfile.pattern, VALUES_DICT["re_catfile"])

	def test_tags_album(self):
		self.assertEqual(self.settings.tags.album, VALUES_DICT["tags_album"])

	def test_types_flac_name(self):
		self.assertEqual(self.settings.types.flac.name, VALUES_DICT["types_flac_name"])

	def test_types__flac_tags_album(self):
		self.assertEqual(self.settings.types.flac.tags.album, VALUES_DICT["types_flac_tags_album"])

	def test_save(self):
		self.settings.save()

		self.assertTrue(True)

if __name__ == '__main__':
	unittest.main()

